<?php

namespace App\Http\Controllers;

use App\Models\Associator;
use Illuminate\Http\Request;
use App\Handlers\ImageUploadHandler;
use Illuminate\Support\Facades\DB;

class ServiceController extends Controller
{
    //
    public function create(Request $request,Associator $user){
        $config = $request->request->all();
        $user_id = ($user->id);
        $video = empty($user->video) ? '':$user->video;
        $voice = empty($user->voice) ? '':$user->voice ;
        $avatar = empty($user->avatar)? $user->portrait:$user->avatar;
        $img = empty($user->img)? '':$user->avatar;
        $service_id = ($request['service_id']);
        $s_name = DB::table('service_manager')->where('id',$service_id)->get();
        $s_name = $s_name[0]->name;
        $config += $config+[
                'video' => $video,
                'voice' => $voice,
            ];
        $configs = json_encode($config);
        $data = [
            's_avatar'=>$avatar,
            'img' => $img,
            'ass_id' => $user_id,
            's_id' => $service_id,
            'name' => $s_name,
            'config' => $configs
        ];
        $a = DB::table('services')->insert($data);
        if ($a){
            $user->update([
               'voice'=>null,
               'avatar'=>null,
               'video'=>null,
               'img' => null
            ]);
            return 1;
        }
        return 0;
    }
    public function getdata(){
        $data =[];
            $data['character'] = \DB::table('character')->get();
            $data['profession'] = \DB::table('profession')->get();
            $data['glamour'] = \DB::table('glamour')->get();
        return $data;
    }
    public function getservice(){
        $data = \DB::table('service_manager')->get();
        return $data;
    }
    public function videoUpload(Associator $user){
        $file = $_FILES['files'];
        //检查文件类型
        $ext = explode('.', $file['name']);
        $ext = end($ext);
        $fname = basename($file['name'],".".$ext);
        $arrExt = array('rmvb','wmv','avi','mp4');
        if(!in_array($ext,$arrExt)) {
            $data['sta'] = FALSE;
            $data['msg'] = '不支持此类型文件的上传！';
            return $data;
        }
        if($file['size']>10485760)
        {
            $data['sta'] = false;
            $data['msg'] = "视频文件过大，只允许10M以内的视频文件！";
            return $data;
        }
        $path = 'uploads/videos/'.date("Ym/d",time())."/";
        if(!file_exists($path))
        {
            mkdir($path,0777,true);
        }
        $fileName = $path.$fname.str_random(10).".".$ext;
        if (!move_uploaded_file($file['tmp_name'],$fileName)){
            $data['sta'] = false;
            $data['msg'] = "视频上传失败，请过一会再试或者联系管理员！";
            return $data;
        }
        $data = array('sta'=>TRUE,'msg'=>'上传成功！','name'=>$file['name']);
        if(!empty($user->video)){
            unlink($user->video);
        }
        $save = $user->update([
            'video' => $fileName
        ]
        );
        if (!$save){
            $data['sta'] = false;
            $data['msg'] = "视频上传失败，请过一会再试或者联系管理员！";
            return $data;
        }
        return $data;
    }
    public function voiceUpload(Associator $user){
        $file = $_FILES['files'];
        //检查文件类型
        $ext = explode('.', $file['name']);
        $ext = end($ext);
        $fname = basename($file['name'],".".$ext);
        $arrExt = array('mp3','jpg');
        if(!in_array($ext,$arrExt)) {
            $data['sta'] = FALSE;
            $data['msg'] = '不支持此类型文件的上传，支持mp3格式！';
            return $data;
        }
        if($file['size']>3145728)
        {
            $data['sta'] = false;
            $data['msg'] = "音频文件过大，只允许3M以内的音频文件！";
            return $data;
        }
        $path = 'uploads/voices/'.date("Ym/d",time())."/";
        if(!file_exists($path))
        {
            mkdir($path,0777,true);
        }
        $fileName = $path.$fname.str_random(10).".".$ext;
        if (!move_uploaded_file($file['tmp_name'],$fileName)){
            $data['sta'] = false;
            $data['msg'] = "音频上传失败，请过一会再试或者联系管理员！";
            return $data;
        }
        $data = array('sta'=>TRUE,'msg'=>'上传成功！','name'=>$file['name']);
        if(!empty($user->voice)){
            unlink($user->voice);
        }
        $save = $user->update([
                'voice' => $fileName
            ]
        );
        if (!$save){
            $data['sta'] = false;
            $data['msg'] = "音频上传失败，请过一会再试或者联系管理员！";
            return $data;
        }
        return $data;
    }

    public function avatarup(Associator $user){
        $file = $_FILES['files'];
        //检查文件类型
        $ext = explode('.', $file['name']);
        $ext = end($ext);
        $fname = basename($file['name'],".".$ext);
        $arrExt = array('jpg','jpeg');
        if(!in_array($ext,$arrExt)) {
            $data['sta'] = FALSE;
            $data['msg'] = '不支持此类型文件的上传！';
            return $data;
        }
        $path = 'uploads/avatars/'.date("Ym/d",time())."/";
        if(!file_exists($path))
        {
            mkdir($path,0777,true);
        }
        $fileName = $path.$fname.str_random(10).".".$ext;
        if (!move_uploaded_file($file['tmp_name'],$fileName)){
            $data['sta'] = false;
            $data['msg'] = "上传失败，请过一会再试或者联系管理员！";
            return $data;
        }
        $data = array('sta'=>TRUE,'msg'=>'上传成功！','name'=>env('APP_URL')."/".$fileName);
        if(!empty($user->avatar)){
            unlink($user->avatar);
        }
        $save = $user->update([
                'avatar' => $fileName
            ]
        );
        if (!$save){
            $data['sta'] = false;
            $data['msg'] = "上传失败，请过一会再试或者联系管理员！";
            return $data;
        }
        return $data;
    }

    public function titleup(Associator $user){
        $file = $_FILES['files'];
        //检查文件类型
        $ext = explode('.', $file['name']);
        $ext = end($ext);
        $fname = basename($file['name'],".".$ext);
        $arrExt = array('jpg','jpeg');
        if(!in_array($ext,$arrExt)) {
            $data['sta'] = FALSE;
            return $data;
        }
        $path = 'uploads/img/'.date("Ym/d",time())."/";
        if(!file_exists($path))
        {
            mkdir($path,0777,true);
        }
        $fileName = $path.$fname.str_random(10).".".$ext;
        if (!move_uploaded_file($file['tmp_name'],$fileName)){
            $data['sta'] = false;
            return $data;
        }
        $data = array('sta'=>TRUE,'name'=>env('APP_URL')."/".$fileName);
        if(!empty($user->img)){
            unlink($user->img);
        }
        $save = $user->update([
                'img' => $fileName
            ]
        );
        if (!$save){
            $data['sta'] = false;
            return $data;
        }
        return $data;
    }
}
